package com.hongyuan.venue.ui.home.view_mode;

import android.app.Application;
import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableList;
import com.hongyuan.venue.R;
import com.hongyuan.venue.base.CustomViewModel;
import com.hongyuan.venue.data.Constants;
import com.hongyuan.venue.data.MyRepository;
import com.hongyuan.venue.ui.home.beans.MemberOsBean;
import java.util.List;
import me.tatarka.bindingcollectionadapter2.BR;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class StoreListViewModel extends CustomViewModel<MyRepository> {

    public StoreListViewModel(@NonNull Application application, MyRepository model) {
        super(application, model);
    }

    //给RecyclerView添加ObservableList
    public ObservableList<StoreListItemViwModel> observableList = new ObservableArrayList<>();

    //给RecyclerView添加ItemBinding
    public ItemBinding<StoreListItemViwModel> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_store_list);


    @Override
    protected void returnData(int code, Object dataBean) {
        super.returnData(code, dataBean);

        if(code == Constants.GET_MEMEBER_OS){
            List<MemberOsBean.ListBean> list = ((MemberOsBean)dataBean).getList();

            if(list != null && list.size() > 0){
                if(curPage == FIRST_PAGE){
                    observableList.clear();
                    addBinding(list);
                }else{
                    addBinding(list);
                }
            }

            if(observableList.size() > 0){
                if(list == null || list.size() == 0){
                    getUC().getFinishLoadMoreData();
                }
            }else{

            }
        }
    }

    /*
     * 添加布局显示
     * */
    private void addBinding(List<MemberOsBean.ListBean> list){
        for(MemberOsBean.ListBean bean : list){
            StoreListItemViwModel itemViewModel = new StoreListItemViwModel(this,bean);
            observableList.add(itemViewModel);
        }
    }
}
